var contractId = "";//合同id
if(window.location.pathname.indexOf("commonDetailPage") > -1){
    $(".last-step").remove();
    $(".cancleEditBtn").show();
    // renderDetail();//详情信息渲染
}else {
    $(".cancleEditBtn").remove();
}
if(thisProviderId&&thisProviderId!=""){
    renderDetail();//详情信息渲染
}
function saveContratFn(stepFn) {
    var params = {
        "contractNumber": $(".contractNumber").val(),
        "contractType": $(".contractType").val(),
        "createUserName": JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
        "effectiveTime": $(".effectiveTime").val(),
        "invalidTime": $(".invalidTime").val(),
        "providerId": thisProviderId,
        "updateUserName": JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
        "contractPdfUrl": $(".contractPdfUrl").attr("imgUrlData")
    }
    if(window.location.pathname.indexOf("commonDetailPage") > -1){
        var doUrl = root.approvedServiceProviders.updateProviderContractById;
        params.id = contractId
    }else {
        var doUrl = root.approvedServiceProviders.addProviderContract;
    }
    var judgmentFieldObj = {
        // "contractNumber":"请输入合同编号",
        // "contractType":"请输入合同类型",
        "effectiveTime":"请选择生效时间",
        "invalidTime":"请选择失效时间",
        "contractPdfUrl":"请上传合同扫描件"
    }
    console.log(params)
    var isContinue = nullValueJudgmentFn(params,judgmentFieldObj);
    if(!isContinue){
        return false;
    }
    web.doAjax(doUrl, 'post',params, function (data) {
        if(data.code == 0){
            console.log(data)
            $.Huimodalalert(data.msg,2000);
            if(window.location.pathname.indexOf("commonDetailPage") > -1){
                $(".form-box").load("detailPackage/contractInfoDetail.html");
            }
            if(stepFn){
                console.log(stepFn)
                eval(stepFn)
            }
        }else {
            $.Huimodalalert(data.msg,2000);
        }
    },1);
}
function submitInfoFn(){
    var params = {
        providerId: thisProviderId
    }
    web.doAjax(root.approvedServiceProviders.submitProvider, 'post',params, function (data) {
        if(data.code == 0){
            console.log(data);
            $.Huimodalalert(data.msg,2000);
            setTimeout(() => {
                if(getQueryString("type")=="edit"){
                    parent.searchList(parent.comPageNum);
                }else {
                    parent.comPageNum = 1;
                    parent.searchList(1);
                }
                cancelLayerNoRefreshFn();
            },2000);
        }else {
            $.Huimodalalert(data.msg,2000);
        }
    },2);
}
timeSet(".effectiveTime",".invalidTime","YYYY-MM-DD");
/*图片上传*/
var formData = new FormData();
function addBtn() {
    formData.delete("file")
    var fileUpEl = document.getElementById('fileUpContractImg') ;
    fileUpEl.outerHTML=fileUpEl.outerHTML;
    $("#fileUpContractImg").trigger('click');
}
function putDataToPage(e,elId) {
    var limitSize = 5*1024*1024;
    for (var i = 0; i < e.target.files.length; i++) {
        console.log(e.target.files)
        var file = e.target.files.item(i);
        if (!(/pdf*$/i.test(file.type))) {
            $.Huimodalalert('请上传5M以内的pdf文件',2000);
            return; //不是图片 就跳出这一次循环
        }
        if(file.size>limitSize){
            $.Huimodalalert('请上传5M以内的pdf文件',2000);
            return;
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function (e) {
            // $("#myImg").attr("src", e.target.result);
        }
    }
    uploadImg(elId);
}
function uploadImg(elId){
    formData.set('file', $("#fileUpContractImg")[0].files[0]);
    $.ajax({
        url: root.inter.imgUpload,
        cache: false, //禁用缓存
        type: "POST",
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        data: formData,
        success: function (data) {
            // console.log(data)
            // console.log(data[0].fullPath)
            $(".contractPdfUrl").attr("src", "../../../img/Pdf.png");
            $(".contractPdfUrl").attr("imgUrlData", data[0].fullPath);
            $(".downloadPdf").attr("imgurldata", data[0].fullPath).show();
            $(".addBtn").html("重新上传");
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    })
}
function pdfShow(el) {
    var pdfUrl = $(el).attr("imgurldata");
    if(pdfUrl&&pdfUrl!=""){
        window.location.href = root.orderInter.downLoadFromUrl +'?urlStr='+pdfUrl+'&fileName=合同扫描件';
    }
}
/*合同信息*/
function renderDetail() {
    //获取详情信息
    var getContractDetail = getContractDetailPro(thisProviderId);
    getContractDetail.then(
        (res) => {
            if (res){
                renderForm(res);
            }
        },  // 成功
        (err) => {console.log(err)} // 失败
    )
}
/*详情信息渲染*/
function renderForm(res){
    $(".contractNumber").val(res.contractNumber);
    $(".contractType").val(res.contractType);
    $(".effectiveTime").val(dealNull(res.effectiveTime));
    $(".invalidTime").val(dealNull(res.invalidTime));
    $(".contractPdfUrl").attr("src","../../../img/Pdf.png");
    $(".contractPdfUrl").attr("imgUrlData",res.contractPdfUrl);
    if(res.contractPdfUrl&&res.contractPdfUrl!=""){
        $(".downloadPdf").attr("imgurldata",res.contractPdfUrl).show();
        $(".addBtn").html("重新上传");
    }
    contractId = res.id;
};
